package org.fanximall.erp.product.application.req;

import com.google.common.base.VerifyException;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.fanximall.erp.common.cqrs.RequestCommand;
import org.fanximall.erp.common.utils.ExceptionUtils;
import org.fanximall.erp.common.valid.ValidGroup;

import javax.validation.constraints.Max;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Null;

/**
 * @author :  william
 * @version :  V1.0  created in 2024/5/6
 */
@Data
public class CategorySaveCommand implements RequestCommand {

    @NotNull(groups = ValidGroup.CrudGroup.Update.class,message = "类目ID不能为空")
    @Null(groups = ValidGroup.CrudGroup.Create.class,message = "类目ID 必须为空")
    private Long categoryId;

    /**
     * 上级类目
     */
    private Long parentId;

    /**
     * 类目中文名
     */
    @NotBlank(message = "类目中文名不能为空")
    private String nameCn;

    /**
     * 类目英文名
     */
    @NotBlank(message = "类目英文名不能为空")
    private String nameEn;

    /**
     * 级别
     */
    @NotNull(message = "类目层级不能为空",groups = ValidGroup.CrudGroup.Create.class)
    @Max(value = 3,message = "类目层级不能超过3级",groups = ValidGroup.CrudGroup.Create.class)
    private Integer level;

    /**
     * 是否是叶子类目 0 否 1是
     */
    @NotNull(message = "是否是叶子类目",groups = ValidGroup.CrudGroup.Create.class)
    private Boolean leaf;


    private LeafCategorySaveCommand leafCategorySaveCommand;


    public void validate() {
        if (this.level > 1) {
            ExceptionUtils.ifEmptyThrowVerifyException(this.parentId, "非一级类目的上级类目不能为空");
        }
        if (this.leaf) {
            ExceptionUtils.ifTrueThrowVerifyException(this.level <3, "叶子类目必须大于二级");
            ExceptionUtils.ifEmptyThrowVerifyException(this.leafCategorySaveCommand, "叶子类目必须填写");
            ExceptionUtils.ifEmptyThrowVerifyException(this.leafCategorySaveCommand.getImage(), "叶子类目图片必须填写");
        }
    }

}
